//遮罩
app.directive('sqmask',function(){
	return {
		controller:function($scope){
			//ui初始化
			this.resizeScope=function(){
				$scope.$apply(function(){
					$scope.uiInit();
				})
			}
		},
		link:function(scope,ele,attrs,ctrl){
			ele.bind('click',function(e){
				//清除遮罩里的全部内容
				if(angular.element(e.target).hasClass('sq-mask'))
				{
					ele.html('');
					ctrl.resizeScope()
				}
			});
		}
	}
})


//防微信朋友圈气泡对话框（点赞/评论）
app.directive('sqchat',function($compile){
	return {
		template:'<i class="iconfont wechat" ng-click="showOrhide()">&#xe60a;</i>'  
					+'<div><div class="chatbox">'
					+'<a ng-click="addOrdelPra()" class="wechat {{heartAni}}">'
					+'<i class="iconfont">&#xe60b;</i>'
					+'{{ct.is_praise==0?"赞":"取消"}}</a>'
					+'<a ng-click="showChat()" class="wechat">'
					+'<i class="iconfont">&#xe60f;</i>'
					+'评论</a>'
					+'</div></div>',
		controller:function($scope,$http){
			$scope.ct.isOpenTool='sq-hide';
			$scope.heartAni='';
			//评论
			$scope.showChat=function(){
				if(!$scope.checksubscrib())
				{
					return ;
				}
				$scope.type="评论";
				$scope.isTie=false;			
//				$scope.uiObj.pageloadingCtrl=false;
				$scope.uiObj.layerloadingCtrl=true;
				$http.get(_sername+'wx_api/Module/community/views/chatview.html'+version).success(function(response){
					var mask=document.getElementsByClassName('sq-mask');
					//重新编译 传入当前分支的$scope
					var el=$compile(response)($scope);
					angular.element(mask).html(el);
					$scope.sqMask.isshow='sq-active';
					$scope.ct.isOpenTool='sq-hide';
					$scope.callSuccessHttp();	
				});
			}
			//点赞or取消
			$scope.addOrdelPra=function(){
				if(!$scope.checksubscrib())
				{
					return ;
				}
				var heartAnimate=function(){
					$scope.heartAni='heartActive';
					setTimeout(function(){
						$scope.heartAni='';
						var method;
						var index=$scope.$index;
						var _sid=$scope.ct.id;
						if($scope.ct.is_praise==0)
						{
							method='addPraise'
						}
						else if($scope.ct.is_praise==1)
						{
							method='addPraise'
						}
						var _link=buildUrl('json','moments',method,{
							actId:$.util.getParamByObject("actId"),
							chanelId:$.util.getParamByObject("chanelId"),
							momentsId:_sid,
							is_admin:document.getElementById("is_admin").value
						})
//						$scope.uiObj.pageloadingCtrl=false;
						$http.get(_link).success(function(data){
							if(data.code==1)
							{
								$scope.pageUi.updateListByPraise(index,data.praise_list,_sid);
								$scope.ct.isOpenTool='sq-hide';

								
							}else{
								$.util.alert(data.msg);
							}
							
							$scope.callSuccessHttp();
							
						});
						
					},200);
				}()
			}
			//开关对话框
			$scope.showOrhide=function(){
					//首先全部关闭
					for(var i=0;i<$scope.data.list.length;i++)
					{
						if($scope.$index!=i)
						{
							$scope.data.list[i].isOpenTool='sq-hide';
						}	
					}
					
					if($scope.ct.isOpenTool=='sq-hide')
					{
						$scope.ct.isOpenTool='sq-active';
					}
					else
					{
						$scope.ct.isOpenTool='sq-hide';
					}
				
			}
			$scope.hideAllwechat=function(){
				   //全部关闭
				    console.log($scope.data.list);
					for(var i=0;i<$scope.data.list.length;i++)
					{
					
						$scope.data.list[i].isOpenTool='sq-hide';
					
					}
				
			}
			$scope.closeChat=function(){
				$scope.uiInit();
				angular.element(document.getElementsByClassName('sq-mask')).html('');
			}
		},
		link:function(s,e,a,c){
				var doc=angular.element(document.getElementsByTagName("body")[0]);
				doc.unbind('touchend click');
				doc.on('touchend click',function(e){
					e=angular.element(e.target);
					
					if(!e.hasClass('wechat'))
					{
						
						s.$apply(function(){
							s.hideAllwechat()
						})
					}
				})
		}
	}
});
//回复别人的评论
app.directive('sqReply',function($compile){
	return {
		controller:function($scope,$http){
			var _this=this;
			//评论
			this.showChat=function(){
				if(!$scope.checksubscrib())
				{
					return ;
				}
				console.log($scope);
				$scope.placeholder="回复"+($scope.cml.nickname!=null?$scope.cml.nickname:"匿名");
				$scope.type="回复";
//				$scope.uiObj.pageloadingCtrl=false;
				$scope.uiObj.layerloadingCtrl=true;
				$http.get(_sername+'wx_api/Module/community/views/chatview.html'+version).success(function(response){
					var mask=document.getElementsByClassName('sq-mask');
					//重新编译 传入当前分支的$scope
					var el=$compile(response)($scope);
					angular.element(mask).html(el);
					$scope.sqMask.isshow='sq-active';
					$scope.ct.isOpenTool='sq-hide';
					$scope.callSuccessHttp();
				});
			}
			$scope.showOrdel=function(isdetail){

				

				//如果是自己的话 就删除回复

				var myuid=document.getElementById('myuid').value;
				
				if($scope.cml.userId!=myuid)
				{
					_this.showChat();
				}
				else
				{
					layer.open({
						style:'width:260px;text-align:center;',
						content:'<div style="height:60px;line-height:60px;font-size: 16px;color:#333;">是否删除这条回复？</div>',
						btn:['确定','取消'],
						shadeClose:false,
						yes: function(){
//					        $scope.uiObj.pageloadingCtrl=false;
							$scope.uiObj.layerloadingCtrl=true;
							$scope.$apply();
							var _link=buildUrl('json','moments','del_comment',{
								actId:$.util.getParamByObject("actId"),
								chanelId:$.util.getParamByObject("chanelId"),
								commentId:$scope.cml.id,
								partakeId:$scope.cml.partakeId
							})
							$http.get(_link).success(function(response){
								console.log(response);
								$scope.pageUi.updateList($scope.$parent.$parent.$index,isdetail);
							});
					    }, no: function(){
					        
					    }
					});
				}
			}
		},
		link:function(s,e,a,c){
			e.on('touchstart',function(){
				e.addClass('touch')
			})
			e.on('touchend touchmove',function(){
				e.removeClass('touch')
			})
		}
	}
})
//右上角更多菜单-视图
app.directive('sqmore',function($compile){
	// var template='<div ng-if="page.topOpen" class="sq-morebox {{sqMore.isshow}}"><ul>'
	// 		+'<li ng-click="showComViews()"><i class="iconfont">&#xe608;</i>发帖</li>'
	// 		+'</ul></div>';
	var template='<a ng-click="showComViews()" class="fatie">'
							+'<i class="iconfont">&#xe608;</i>'
							+'</a>';
	return {
		replace:true,
		template:template,
		controller:function($scope,$http){
			$scope.showComViews=function(){
				if(!$scope.checksubscrib())
				{
					return ;
				}
				$scope.type="发表";
				$scope.isTie=true;
//				$scope.uiObj.pageloadingCtrl=false;
				$scope.uiObj.layerloadingCtrl=true;
				$http.get(_sername+'wx_api/Module/moments/views/chatview.html'+version).success(function(response){
					var mask=document.getElementById('tie')
					//重新编译 传入当前分支的$scope
					var el=$compile(response)($scope);
					angular.element(mask).html(el);
					$scope.callSuccessHttp();
					callback();
				});
				
				var callback=function(){
					$scope.sqCom.isshow='active';
					$scope.uiInit();
					setTimeout(function(){
						$scope.uiObj.isactive='sq-hide';
						$scope.$apply();
					},300)
				}
				
			}
			
		}
	}
})
//右上角更多菜单-逻辑
app.directive('sqmorectrl',function(){
	return {
		require:'sqmorectrl',
		controller:function($scope){
			this.showOrhide=function(ele){
				
				var height;
				if($scope.sqMore.isshow=='sq-hide')
				{
					$scope.sqMore.isshow='sq-active';

				}
				else
				{
					$scope.sqMore.isshow='sq-hide';
									
				}
			}
		},
		link:function(scope,ele,attrs,ctrl){
			var _this=this;
			ele.bind('click',function(){
				scope.$apply(ctrl.showOrhide(ele));
			})
		}
	}
})
//左上角侧边栏
app.directive('sqsidectrl',function(){
	return {
		require:'sqsidectrl',
		controller:function($scope){
			console.log('-----------');
			console.log($scope);
			this.showOrhide=function(ele){
				var height;
				if($scope.sqSide.isshow=='sq-hide')
				{
//					$scope.uiObj.pageloadingCtrl=true;
					$scope.sqSide.isshow='sq-active';

				}
				else
				{
					$scope.sqSide.isshow='sq-hide';
									
				}
				$scope.sqMask.isshow=$scope.sqSide.isshow;
				
			}
		},
		link:function(scope,ele,attrs,ctrl){
			ele.bind('click',function(){
				
				scope.$apply(ctrl.showOrhide(ele));
			})
		}
	}
})

//删除帖子 文章 广告
app.directive('sqDeleted',function(){
	return {
		controller:function($scope,$http){
			$scope.deleted=function(){
				layer.open({
						style:'width:260px;text-align:center;',
						content:'<div style="height:60px;line-height:60px;font-size: 16px;color:#333;">是否删除这条帖子？</div>',						
						btn:['确定','取消'],
						shadeClose:false,
						yes: function(){
//					        $scope.uiObj.pageloadingCtrl=false;
							$scope.uiObj.layerloadingCtrl=true;
							$scope.$apply();
							var _link=buildUrl('json','community','deleted',{
								id:$scope.ct.id
							})
							
							$http.get(_link).success(function(response){
								console.log(response);
								layer.open({
									content:'删除成功!'
								})
								location.reload();
							});
					    }, no: function(){
					        
					    }
					});
			}
		}
	}
})
//置顶
app.directive('topopt',function(){
	return {
		controller:function($scope,$http){
			var _sid='',
				index=$scope.$index;
			if(typeof($scope.ct)=="undefined")
			{
				_sid=$scope.cl.sid
			}
			else
			{
				_sid=$scope.ct.id
			}
			this.putOrcanceltop=function(method,text){
				layer.open({
						style:'width:260px;text-align:center;',
						content:'<div style="height:60px;line-height:60px;font-size: 16px;color:#333;">是否'+text+'置顶？</div>',						
						btn:['确定','取消'],
						shadeClose:false,
						yes: function(){
//					        $scope.uiObj.pageloadingCtrl=false;
							$scope.uiObj.layerloadingCtrl=true;
							$scope.$apply();
							var _link=buildUrl('json','moments',method,{
								actId:$.util.getParamByObject("actId"),
								chanelId:$.util.getParamByObject("chanelId"),
								momentsId:_sid
							})
							console.log(_link);
							$http.get(_link).success(function(data){
//								
//								if($scope.ct.type==0||$scope.ct.type==2)
//								{
//									if($scope.ct.isTop!=null)
//									{
//										$scope.pageUi.isTopCount--;
//									}
//									else
//									{
//										$scope.pageUi.isTopCount++;
//									}
//									
//								}
								
								if(data.code==1)
								{
									location.reload();
								}
								
								
							});
					    }, no: function(){
					        
					    }
					});
					
					
				
			}
		},
		link:function(s,e,a,c){
			
			e.on('click',function(){
//				if(s.uiObj.pageloadingCtrl)
//				{
					if(e.hasClass('putTop'))
					{
						c.putOrcanceltop('add_top','确认');
					}
					else if(e.hasClass('cancelTop'))
					{
						c.putOrcanceltop('add_top','取消');
					}
					
//				}
				
			})
		}
	}
})
app.directive('sqShowImg',function(){
	return {
		controller:function($scope,$http){
			$scope.showImgsWx=function(){
				console.log($scope);
				wx_share.showImg($scope.ct.picUrls,$scope.pic);
			}
		},
		link:function(){
			
		}
	}
})
//切换表情 
app.directive('sqTabs',["$rootScope",function($rootScope){
	return {
		restrict:'C',
		controller:function(){
			
		},
		link:function(s,e,a,c){
			
			e.find('li a').on('click',function(e){
					e.preventDefault()
					angular.element(this).tab('show');	
					$rootScope.updateSwiper();
			})
		}
	}
}]);
app.directive('cashTab',["$rootScope",function($rootScope){
	return {
		controller:function(){
			
		},
		link:function(s,e,a,c){
			if(s.$last)
			{
				e.parent().find('a').on('click',function(e){
					e.preventDefault()
					angular.element(this).tab('show');	
				})
			}
			
		}
	}
}]);
//swiper 插件 
app.directive('swiperDirective',["$rootScope",function($rootScope){
	return {
		controller:function(){
			this.ready=function(){
				$rootScope.initSwiper('.swiper-container');
			}
		},
		link:function(s,e,a,c){
		}
	}
}]);
app.directive('swiperSlide',function(){
	return {
        restrict: "A",
        require: "^swiperDirective",
        link: function(s,e,a,c) {
            if(s.$last) {
                c.ready();
            }
        }
    }
})
app.directive('sqWximgs',function(){
	return {
		controller:function($scope,$http){
			this.remove=function(){
				$scope.wxImgs.imgs.splice($scope.$index,1);
				$scope.wxImgs.upload=true;
			}
		},
		link:function(s,e,a,c){
			e.on('click',function(){
				c.remove();
				e.parent().remove();
			})
		}
	}
})
//发起评论or帖子
app.directive('sqMychat',function(){
	return {
		restrict:'C',
		controller:function($scope,$http,$rootScope){
			console.log($scope);
			$scope.imgs={
					paths:''
			}
			var width=document.body.clientWidth;
			if(width>=375)
			{
				$scope.chatsize="big";
			}
			else
			{
				$scope.chatsize="small";
			}
			$scope.postComment=function(){
				var replyid='';
				if($scope.isTie)
				{
					//发帖
					var _link=buildUrl('json','moments','save_data',{
						actId:$.util.getParamByObject("actId"),
						chanelId:$.util.getParamByObject("chanelId"),
						content:$scope._comment?$scope._comment.replace(/\n|\r\n/g,"<br>"):"",
						picUrls:$scope.imgs.paths,
						is_admin:document.getElementById("is_admin").value
					})
//					alert($scope.imgs.paths);
				}
				else
				{
					if(typeof($scope.cml)!="undefined")
					{
						replyid=$scope.cml.id;
					}
					//评论或者回复
					var _link=buildUrl('json','moments','publish_comment',{
						actId:$.util.getParamByObject("actId"),
						chanelId:$.util.getParamByObject("chanelId"),
						momentsId:$scope.ct.id,
						comment:$scope._comment?$scope._comment.replace(/\n|\r\n/g,"<br>"):"",
						is_admin:document.getElementById("is_admin").value,
						replyId:replyid
					})
				}
//				$scope.uiObj.pageloadingCtrl=false;
				$scope.uiObj.layerloadingCtrl=true;
				if(($scope._comment==''||typeof($scope._comment)=="undefined")&&!$scope.imgs.paths)
				{
					layer.open({
							content:'请输入内容'
					})
				}
				else if($rootScope.uiObj.is_send_location=="1"&&$scope.isTie)
				{


					wx_share.getLocation(function(latitude,longitude,speed,accuracy){
							//发帖
							var __link=buildUrl('json','moments','save_data',{
								actId:$.util.getParamByObject("actId"),
								chanelId:$.util.getParamByObject("chanelId"),
								content:$scope._comment?$scope._comment.replace(/\n|\r\n/g,"<br>"):"",
								picUrls:$scope.imgs.paths,
								is_admin:document.getElementById("is_admin").value,
								lat:latitude?latitude:"",
								lng:longitude?longitude:""
							})
							$http.get(__link).success(function(data){
							console.log(data);
								if(data.code==1)
								{
									if(replyid!='')
									{
										var _index=$scope.$parent.$index
									}
									else
									{
										var _index=$scope.$index
									}
									if(!$scope.isTie)
									{
										//更新并初始化 包含callSuccessHttp
										$scope.pageUi.updateList(_index);
									}
									else
									{
										$scope.callSuccessHttp();
										layer.open({
											content:'发帖成功，可能还需等待管理员审核通过方可显示。',
											shadeClose:false,
											btn:["确定"],
											yes:function(){
												location.reload();
											}
										})
									}
								}
								else
								{
									layer.open({
										content:data.msg
									})
								}
								//清空内容
								$scope._comment='';
							});
					})
					
				} else {
					$http.get(_link).success(function(data){
					console.log(data);
						if(data.code==1)
						{
							if(replyid!='')
							{
								var _index=$scope.$parent.$index
							}
							else
							{
								var _index=$scope.$index
							}
							if(!$scope.isTie)
							{
								//更新并初始化 包含callSuccessHttp
								if($scope.page.isdetail)
								$scope.pageUi.updateList(_index,$scope.ct.id);
								else
								$scope.pageUi.updateList(_index);
							}
							else
							{
								$scope.callSuccessHttp();
								layer.open({
									content:'发帖成功，可能还需等待管理员审核通过方可显示。',
									shadeClose:false,
									btn:["确定"],
									yes:function(){
										location.reload();
									}
								})
							}
						}
						else
						{
							layer.open({
								content:data.msg
							})
						}
						//清空内容
						$scope._comment='';
					});
				}
			}
			this.putface=function(mark){
				console.log($scope);
				if(typeof($scope._comment)=="undefined")
				{
					$scope._comment="";
				}
				$scope._comment=$scope._comment+mark;
			};
		},
		link:function(s,e,a,c){
		}
	}
})
//注入表情
app.directive('sqFace',function(){
	return {
		require:'^sqMychat',
		controller:function($scope,$http){
			//返回mark
			$scope.getmark=function(){
				return $scope.g.mark;
			}
		},
		link:function(s,e,a,c){
			e.on('click',function(event){
//				event.preventDefault();
				var mark=s.getmark();
				c.putface(mark);
			})
		}
	}
})
app.directive('sqForm',["$timeout",function($timeout){
	return {
		link:function(s,e,a,c){
			if(!s.isTie)
			{
			 $timeout(function(){
                e[0].focus();
             })
			}
		}
	}
}])
app.directive('goArticle',function(){
	return {
		controller:function($scope,$http){
			this.goarticle=function(){
				console.log($scope);
				var _qId=document.getElementById('qid').value,_id,_miUId,type;
				_miUId=document.getElementById('miUId').value;
				if($scope.cl)
				{
					type=$scope.cl.type;
					_id=$scope.cl.id;
					fromCustomerId=$scope.cl.fromCustomerId;
				}
				else
				{
					type=$scope.ct.type;
					_id=$scope.ct.id;
					fromCustomerId=$scope.ct.fromCustomerId;
				}
				if(type==0||type==null)
				{
					var _link=buildUrl('html','micontent','getMiContentDetail',{
						qId:_qId,
						id:_id,
						miUId:_miUId
					},fromCustomerId)
					window.location.href=serhost+_link;
				}
			}
		},
		link:function(s,e,a,c){
			e.on('click',function(){
				c.goarticle();
			})
		}
	}
})
//我的用户，选择筛选 遮罩控制
app.directive('selectMask',function(){
	return {
		controller:function($scope,$http){
			$scope.hideMask=function(){
				$scope.pageUi.mask='sq-hide';
				for(var i=0;i<$scope.pageUi.content.length;i++)
				{
					$scope.pageUi.content[i].selectNav="sq-hide";
				}
			}
		}
	}
})
//我的用户，选择筛选交互
app.directive('sqSelectbtn',function(){
	return {
		controller:function($scope,$http){
			
			if($scope.pageUi.dataType==0)
			{
				var a=getCookie('extension');
				if(a!=null)
				{
					a=a.split(',');
					console.log(a);
					if(a[$scope.$index]=='')
					{
						a[$scope.$index]=0;
					}
					
//					$scope.pageUi.content[$scope.$index].name=$scope.s.list[$scope.s.activeselect].name;
					
					$scope.s.activeselect=a[$scope.$index];
					
				}
				else
				{
					$scope.s.activeselect=$scope.pageUi.activeselect;
				}
			}
			else
			{
				var a=getCookie('bulproduct');
				if(a!=null)
				{
					a=a.split(',');
					console.log(a);
					if(a[$scope.$index]=='')
					{
						a[$scope.$index]=0;
					}
					
//					$scope.pageUi.content[$scope.$index].name=$scope.s.list[$scope.s.activeselect].name;
					
					$scope.s.activeselect=a[$scope.$index];
					
					
					//判断供应商第一项是否选择商城
					if($scope.$index==0)
					{
						if(a[$scope.$index]!=0)
						{
							$scope.pageUi.isupdate=true;
						}
					}
				}
				else
				{
					$scope.s.activeselect=$scope.pageUi.activeselect;
				}
				
			}
//			$scope.s.activeselect=$scope.pageUi.activeselect;
			$scope.allSelectHide=function(){
				for(var i=0;i<$scope.pageUi.content.length;i++)
				{
					$scope.pageUi.content[i].selectNav="sq-hide";
				}
			}
			$scope.select=function(){
				if($scope.s.selectNav=="sq-hide")
				{
					$scope.allSelectHide();
					$scope.s.selectNav="sq-active";
					$scope.pageUi.mask='sq-active';
				}
				else
				{
					$scope.s.selectNav="sq-hide";
					$scope.pageUi.mask='sq-hide';
				}
			}
		}
	}
})
//分销商品--查看商品(返利)
app.directive('goprodetail',function(){
	return {
		controller:function($scope){
			$scope.goprodetail=function(){
				var _link=buildUrl('html','product','getProduct',{
					id:$scope.l.id,
					qId:document.getElementById('qid').value,
					sId:$scope.l.sId,
					miUId:document.getElementById('miUId').value,
					
				},$scope.l.fromCustomerId)
				window.location.href=_link;
			}
		}
	}
})

//邀请-用户列表分页
app.directive('refereePage',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http){
			$scope.isloaded=true;
			this.getlist=function(){
				if($scope.pageUi.iswork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载..';
					$scope.isloaded=false;
					var _link=buildUrl('json','community','refereeInfo',{
						qid:document.getElementById('qid').value,
						start:$scope.pageUi.start,
						limit:$scope.pageUi.limit
					})
					$http.get(_link).success(function(response){
						if(response.refereeList!=null)
						{
							if($scope.pageUi.data!=null)
							{
								if(typeof($scope.pageUi.data.refereeList)!="undefined")
								{
									for(var i=0;i<response.refereeList.length;i++)
									{
											
										$scope.pageUi.data.refereeList.push(response.refereeList[i]);
									}
								}
							}
							//第一次
							else
							{
								
								$scope.pageUi.data=response;
								
							}
							console.log(response);
							
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							if(response.refereeList.length<$scope.pageUi.limit)
							{
								$scope.pageUi.iswork=false;
								$scope.pageUi.text='无更多数据';
							}
							else
							{
								$scope.pageUi.text='下拉加载更多';
							}
						}
						else
						{
							//quanInfo也要导入
							if($scope.pageUi.data)
							{
								$scope.pageUi.data.quanInfo=response.quanInfo;
							}
							else
							{
								$scope.pageUi.data={}
								$scope.pageUi.data.quanInfo=response.quanInfo;
							}
							$scope.pageUi.iswork=false;
							$scope.pageUi.text='无更多数据';
							
						}
						if(!$scope.pageUi.isShare)
						{
							$scope.pageUi.isShare=true;
//							shareData.link=response.quanInfo.refereeUrl;
//							wx_share.init(shareData);
						}
						$scope.isloaded=true;
						$scope.callSuccessHttp();
					});
					
				}
			}
		},
		link:function(s,e,a,c){
			
			c.getlist();
			window.onscroll = function(){
				if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
					c.getlist();
				}
			}
		}
	}
}])
//我的团队列表
app.directive('teamList',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http,$compile,$routeParams){
			$scope.isloaded=true;
			this.getlist=function(){
				if($scope.pageUi.iswork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载..';
					$scope.isloaded=false;
					var _link=buildUrl('json','community','teamUser',{
						teamId:document.getElementById('teamId').value,
//						uid:document.getElementById('uid').value,
						uid:$routeParams.uid?$routeParams.uid:document.getElementById('uid').value,
						start:$scope.pageUi.start,
						limit:$scope.pageUi.limit
					})
					$http.get(_link).success(function(response){
						if(response!=null)
						{
							if($scope.pageUi.data!=null)
							{
									for(var i=0;i<response.length;i++)
									{
											
										$scope.pageUi.data.push(response[i]);
									}

							}
							else
							{
								$scope.pageUi.data=response;
							}
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							if(response.length<$scope.pageUi.limit)
							{
								$scope.pageUi.iswork=false;
								$scope.pageUi.text='无更多数据';
							}
							else
							{
								$scope.pageUi.text='下拉加载更多';
							}
						}
						$scope.isloaded=true;
						console.log($scope.pageUi.data);
					})
				}
			}
		},
		link:function(s,e,a,c){
			
			c.getlist();
			window.onscroll = function(){
				if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
					c.getlist();
				}
			}
		}
	}
}]);
//我的好友列表
app.directive('fansList',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http,$compile,$routeParams){
			$scope.isloaded=true;
			this.getlist=function(){
				if($scope.pageUi.iswork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载..';
					$scope.isloaded=false;
					var _link=buildUrl('json','community','fansList',{
						page:$rootScope.uiObj.fansPage,
						mallId:document.getElementById('mallId').value,
						uid:document.getElementById('uid').value,
//						uid:document.getElementById('uid').value,
						qid:document.getElementById('qid').value,
						start:$scope.pageUi.start,
						limit:$scope.pageUi.limit
					})
					
					$http.get(_link).success(function(response){
						
						if(response.userList!=null)
						{
							$scope.pageUi.level=response.level;
							$rootScope.uiObj.fansPage=response.page;
							response=response.userList;
							if($scope.pageUi.data!=null)
							{
								for(var i=0;i<response.length;i++)
								{
									$scope.pageUi.data.push(response[i]);
								}
							}
							else
							{
								$scope.pageUi.data=response;
							}
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							if(response.length<$scope.pageUi.limit)
							{
								$scope.pageUi.iswork=false;
								$scope.pageUi.text='无更多数据';
							}
							else
							{
								$scope.pageUi.text='下拉加载更多';
							}
						}
						else
						{
							$scope.pageUi.iswork=false;
							$scope.pageUi.text='无更多数据';
						}
						$scope.isloaded=true;
					})
				}
			}
		},
		link:function(s,e,a,c){
			c.getlist();
			window.onscroll = function(){
				if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
					c.getlist();
				}
			}
		}
	}
}]);
//获取好友数量
app.directive('getFanscount',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http,$compile,$routeParams){
			$scope.fansConfig={
				isAlready:false,
				data:null
			}
			var _link=buildUrl('json','community','getFansCount',{
					qid:document.getElementById('qid').value
			})
			$http.get(_link).success(function(response){
				$scope.fansConfig.data=response;
				console.log(response);
				$scope.fansConfig.isAlready=true;
			})
		}
		
	}
}]);
app.directive('viewList',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http,$compile,$routeParams){
			$scope.isloaded=true;
			$scope.hhrtag=document.getElementById('hhrtag').value;
			$scope.mytag=document.getElementById('mytag').value;
			this.getlist=function(){
				if($scope.pageUi.iswork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载..';
					$scope.isloaded=false;
					if($scope.type=='team')
					{
						var _link=buildUrl('json','community','teamUser',{
							teamId:document.getElementById('teamId').value,
	//						uid:document.getElementById('uid').value,
							uid:$routeParams.uid,
							start:$scope.pageUi.start,
							limit:$scope.pageUi.limit
						})
					}
					else if($scope.type=='myfans')
					{
						var _link=buildUrl('json','community','fansList',{
							page:$rootScope.uiObj.fansPage,
							mallId:document.getElementById('mallId').value,
							uid:$routeParams.uid,
	//						uid:document.getElementById('uid').value,
							qid:document.getElementById('qid').value,
							start:$scope.pageUi.start,
							limit:$scope.pageUi.limit
						})
					}
					$http.get(_link).success(function(response){
						var tmp;
						if($scope.type=="team")
						{
							tmp=response;
						}
						else if($scope.type=="myfans")
						{
							tmp=response.userList;
							$scope.pageUi.level=response.level;
							$rootScope.uiObj.fansPage=response.page;
							if($scope.pageUi.level==3)
							{
								$scope.pageUi.clickactive=false;
							}
						}
						if(tmp!=null&&tmp)
						{
							response=tmp;
							if($scope.pageUi.data!=null)
							{
									for(var i=0;i<response.length;i++)
									{
											
										$scope.pageUi.data.push(response[i]);
									}

							}
							else
							{
								$scope.pageUi.data=response;
							}
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							if(response.length<$scope.pageUi.limit)
							{
								$scope.pageUi.iswork=false;
								$scope.pageUi.text='无更多数据';
							}
							else
							{
								$scope.pageUi.text='下拉加载更多';
							}
						}
						else
						{
							$scope.pageUi.iswork=false;
							$scope.pageUi.text='无更多数据';
						}
						$scope.isloaded=true;
						console.log($scope.pageUi.data);
					})
				}
			}
		},
		link:function(s,e,a,c){
			
			c.getlist();
			window.onscroll = function(){
				if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
					c.getlist();
				}
			}
		}
	}
}]);
//树级列表条目
app.directive('treeList',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http,$compile,$rootScope,$routeParams,$cacheFactory){
			$scope.nextTree=function(){
				var type;
				if(type=="myfans")
				{
					if($scope.pageUi.level<3)
					{
						$scope.pageUi.clickactive=false;
					}
				}
				if($scope.t.child!=0&&$scope.pageUi.clickactive)
				{
					$rootScope.uiObj.pageAnimationForce=true;
					$rootScope.uiObj.pageAnimationClass='slideRight';
					console.log($rootScope.uiObj.pageAnimationClass);
					if(typeof($routeParams.type)=="undefined")
					{
						type=$scope.pageUi.pageType;
					}
					else
					{
						type=$routeParams.type;
					}
					if(type=='myfans')
					{
						if(!$rootScope.uiObj.fansTitleCache.get('uid:'+$scope.t.uid))
						{
							var  nickname;
							if($scope.t.nickName)
							{
								nickname=$scope.t.nickName;
							}
							else
							{
								nickname=$scope.t.nickname;
							}
							if(nickname.length>7)
							{
								nickname=nickname.substring(0,7)+'...';
							}
							$rootScope.uiObj.fansTitleCache.put('uid:'+$scope.t.uid,nickname+'的好友');
						}
						
//						$scope.page.title=$scope.t.nickName+'的好友';
					}
					setTimeout(function(){
						location.href='#/treepage/'+type+'/'+$scope.t.uid;
					},10)
					
				}
				
			}
			$scope.deleted=function(id,index){
				layer.open({
					style:'width:260px;text-align:center;',
					content:'<div style="height:60px;line-height:60px;font-size: 16px;color:#333;">是否将此人从团队删除？</div>',
					btn:['确定','取消'],
					shadeClose:false,
					yes: function(){
						var _link=buildUrl('json','community','deletedTeamUser',{
							id:id
						});
						$http.get(_link).success(function(data){
							layer.closeAll();
							if(data.code!=0){
								layer.open({
									content:data.msg
								});
								return;
							}
							$scope.pageUi.data.splice(index,1);
							var _link=buildUrl('json','community','quanTeam',{
								teamId:document.getElementById('teamId').value
							})
							$http.get(_link).success(function(response){
								for(var o in response){
									$scope.data[o]=response[o];
								}
							});
						});
					}, no: function(){

					}
				});

			}
		}
	}
}])

//我的用户，选择筛选
app.directive('sqSelectIn',["$rootScope",function($rootScope){
	return {
		controller:function($scope,$http){
			var _this=this;
			if($scope.$parent.$index==0)
			{
				var f_cid=$scope.v.customerId;
			}
			
//			if($scope.$last)
//			{
//				
//				alert($scope.s.activeselect);
//			}

			$scope.s.name=$scope.s.list[$scope.s.activeselect].name;
			
	
			$scope.selectIn=function(){
				
				if($scope.pageUi.dataType==0)
				{
					
					if(typeof($scope.v.customerId)!="undefined")
					{
						$scope.pageUi.cid=$scope.v.customerId;
					}
					if(typeof($scope.v.type)!="undefined")
					{
						$scope.pageUi.type=$scope.v.type;
					}
				}
				else
				{
					//商品分销供应商选择
					$scope.pageUi.keyName='';
					if($scope.$parent.$index==0)
					{
						$scope.pageUi.vid=$scope.v.id;
						$scope.pageUi.tagId='';
						$scope._params.tagId='';
						if($scope.$index==0)
						{
							$scope.pageUi.isupdate=false;
						}
						else
						{
							$scope.pageUi.isupdate=true;
						}
						$scope.pageUi.selectinit=true;
						$scope.pageUi.content[1].list=null;
						$scope.pageUi.content[1].name="商品分类";
						$scope.uiObj.layerloadingCtrl=true;
					}
					else if($scope.$parent.$index==1)
					{
						$scope.pageUi.tagId=$scope.v.id;
						$scope.pageUi.selectinit=false;
					}
					else if($scope.$parent.$index==2)
					{
						$scope.pageUi.sort=$scope.v.type;
					}
				}
				$scope.s.activeselect=$scope.$index;
				$scope.s.name=$scope.v.name;
				
				
				if($scope.pageUi.dataType!=0)
				{
					if($scope.$parent.$index==0)
					{
						bulselectlist[1]=0;
					}
					
					bulselectlist[$scope.$parent.$index]=$scope.s.activeselect;
					
					
					bulselectstr='';
					for(var i=0;i<bulselectlist.length;i++)
					{
						if(i!=0)
						{
							tmp=',';
						}
						else
						{
							tmp='';
						}
						bulselectstr=bulselectstr+tmp+bulselectlist[i];
					}
					setCookie('bulproduct',bulselectstr,'d30');
				}
				else
				{
					exselectlist[$scope.$parent.$index]=$scope.s.activeselect;
					
					exselectstr='';
					for(var i=0;i<exselectlist.length;i++)
					{
						if(i!=0)
						{
							tmp=',';
						}
						else
						{
							tmp='';
						}
						exselectstr=exselectstr+tmp+exselectlist[i];
					}
					setCookie('extension',exselectstr,'d30');
				}
				
				
				
				
				//http部分
				$scope.pageUi.isSelect=true;
				_this.getlist();
//				console.log("cid:"+$scope.pageUi.cid+',type:'+$scope.pageUi.type+',vid:'+$scope.pageUi.vid+',tagId:'+$scope.pageUi.tagId+',sort:'+$scope.pageUi.sort)
			}
			this.getlist=function(){
				console.log($scope._params);
				if($scope.pageUi.isSelect)
				{
					$scope.pageUi.data='';
					$scope.pageUi.init();
					//若操作筛选时，选择参数为操作过的那个值优先
					if($scope.pageUi.cid!=null)
					{
						$scope._params.cid=$scope.pageUi.cid
					}
					if($scope.pageUi.type!=null)
					{
						$scope._params.type=$scope.pageUi.type
					}
					if($scope.pageUi.vid!=null)
					{
						$scope._params.vid=$scope.pageUi.vid
					}
					
					if($scope.pageUi.tagId!=null)
					{
						$scope._params.tagId=$scope.pageUi.tagId
					}
					
					if($scope.pageUi.sort!=null)
					{
						$scope._params.sort=$scope.pageUi.sort
					}
					$scope.pageUi.isSelect=false;
				}
				else
				{
					//若没发生操作,则缓存里的参数优先.
					$scope._params.cid=((getCookie('cid')=="null"||getCookie('cid')==null)?f_cid:getCookie('cid'));
					$scope._params.type=(getCookie('type')=="null"?null:getCookie('type'));
					$scope._params.vid=(getCookie('vid')=="null"?null:getCookie('vid'));
					$scope._params.tagId=(getCookie('tagId')=="null"?null:getCookie('tagId'));
					$scope._params.sort=(getCookie('sort')=="null"?null:getCookie('sort'));
				}
				if($scope.pageUi.dataType==0)
				{
					var _link=buildUrl('json','community','recruitUserList',{
						qid:document.getElementById('qid').value,
						cid:$scope._params.cid!=null?$scope._params.cid:'',
						type:$scope._params.type!=null?$scope._params.type:0,
						start:$scope.pageUi.start,
						limit:$scope.pageUi.limit
					})
					
					//最终发生请求时,将最终值存入缓存中.
					setCookie('cid',$scope._params.cid,'d30');
					setCookie('type',$scope._params.type,'d30');
				}
				else
				{

					
					var _link=buildUrl('json','community','getProductByCondition',{
						qid:document.getElementById('qid').value,
						vid:$scope._params.vid!=null?$scope._params.vid:'',
						tagId:$scope._params.tagId!=null?$scope._params.tagId:'',
						sort:$scope._params.sort!=null?$scope._params.sort:0,
						start:$scope.pageUi.start,
						limit:$scope.pageUi.limit
					})
					//最终发生请求时,将最终值存入缓存中.
					setCookie('vid',$scope._params.vid,'d30');
					setCookie('tagId',$scope._params.tagId,'d30');
					setCookie('sort',$scope._params.sort,'d30');
				}
				console.log('cid:'+$scope._params.cid+',type:'+$scope._params.type+',vid:'+$scope._params.vid+',tagId:'+$scope._params.tagId+',sort:'+$scope._params.sort)
				console.log('cid:'+getCookie('cid')+',type:'+getCookie('type')+',vid:'+getCookie('vid')+',tagId:'+getCookie('tagId')+',sort:'+getCookie('sort'))
//				$scope.uiObj.pageloadingCtrl=false;
				if($scope.pageUi.isWork){
					$scope.pageUi.text='正在加载...';
					$scope.pageUi.mallload=true;
					$http.get(_link).success(function(response){
						if(typeof(response.userList)!="undefined"||typeof(response.productList)!="undefined")
						{
							var KEY=typeof(response.userList)!="undefined"?"userList":"productList";
							//有数据
							if(response.userList!=null||response.productList!=null)
							{
															
								if($scope.pageUi.isFirst)
								{
									$scope.pageUi.data = response;
									$scope.pageUi.isFirst=false;
									if(response[KEY].length<$scope.pageUi.limit)
									{
										$scope.pageUi.isWork=false;
										$scope.pageUi.text='没有更多数据';
									}
								}
								else
								{
									for(var i=0;i<response[KEY].length;i++)
									{
										
										$scope.pageUi.data[KEY].push(response[KEY][i]);
									}
								}
								$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
								if($scope.pageUi.isWork)
								{
									$scope.pageUi.text='下拉加载';
								}
								
							}
							else
							{
								if($scope.pageUi.dataType==0&&$scope.pageUi.isFirst)
								{
									$scope.pageUi.data={
										count:'',
										mallUrl:''
									}
									$scope.pageUi.data.count = response.count;
									$scope.pageUi.data.mallUrl = response.mallUrl;
								}
								
								$scope.pageUi.isWork=false;
								$scope.pageUi.text='没有更多数据';
							}
						}
						else
						{
							$scope.pageUi.text='';
						}
						
						
						//选择供应商，分类列表需要更新distributionList,不管userList和productList是否值
							    if(KEY=="productList"&&$scope.$parent.$index==0&&$scope.pageUi.isupdate)
								{
									if(response.distributionList)
									{
										$scope.pageUi.content[1].list=response.distributionList;
									}
									else
									{
										$scope.pageUi.content[1].list=new Array();
									}
									$scope.pageUi.content[1].activeselect=0;
									$scope.pageUi.content[1].list.unshift({
										id:'',
										name:'全部',
										logo:1
									})
									
									//更新完后 重新提取下cookies记忆
									var a=getCookie('bulproduct');
									if(a!=null)
									{
										a=a.split(',');
										console.log(a);
										if(a[1]=='')
										{
											a[1]=0;
										}
										
					//					$scope.pageUi.content[$scope.$index].name=$scope.s.list[$scope.s.activeselect].name;
										$scope.pageUi.content[1].activeselect=a[1];										
									}
									else
									{
										$scope.pageUi.content[1].activeselect=$scope.pageUi.activeselect;
									}
								}		
						$scope.pageUi.mallload=false;
						$scope.callSuccessHttp();
					});
				}
			}
		},
		link:function(s,e,a,c){
			if(s.$last&&s.$parent.$index==0)
			{
				c.getlist();
				//监听滚动条
				 window.onscroll = function()
				{
					s.pageUi.isSelect=false;
				　　  if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
					{
					　　　c.getlist();
					}
				}
			}
		}
	}
}])

app.directive('listenBlance',function(){
	return {
		controller:function($scope,$http,$cacheFactory,$routeParams){
			$scope.detailType=parseInt($routeParams.type)+1;
			$scope.showDetail=function(type){
				$scope.detailType=parseInt(type)+1;
			}
			this.getmore=function(){
				var dt=$scope.pageUi.balancelist[$scope.detailType-1];
				if(dt.loadlist&&dt.iswork)
				{
					dt.loadlist=false;
					var _link=buildUrl('json','quan','getBalanceList',{
						qId:document.getElementById('qid').value,
						uType:1,
						tType:$scope.detailType,
						start:dt.start,
						limit:dt.limit
					})
					$http.get(_link).success(function(response){
						//有数据
						if(response.rs)
						{
							
							for(var i=0;i<response.rs.length;i++)
							{
								dt.balancelist.rs.push(response.rs[i]);
							}
							dt.start=dt.start+dt.limit;
							console.log($scope.pageUi.balancelist);
							
						}
						//无数据
						else
						{
							dt.iswork=false;
						}
						dt.loadlist=true;
						$scope.callSuccessHttp();
						
						
					})
				}
				
			}
		},
		link:function(s,e,a,c){
			window.onscroll = function()
			{
			    if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
				    c.getmore();
				}
			}
				
		}
	}
})


//我的收入 明细列表
app.directive('balanceList',function(){
	return {
		controller:function($scope,$http,$cacheFactory,$routeParams){
			var myIdentity=document.getElementById('myIdentity').value;
				if(myIdentity==0)
				{
					var _uType=2;
				}
				else if(myIdentity==1)
				{
					var _uType=1;
				}
			this.getlist=function(_tType){
				var _link=buildUrl('json','quan','getBalanceList',{
					qId:document.getElementById('qid').value,
					uType:1,
					tType:_tType,
					start:$scope.p.start,
					limit:$scope.p.limit
				})
				$scope.p.loadlist=false;
				$http.get(_link).success(function(response){
					
					$scope.p.loadlist=true;
					$scope.p.balancelist=response;
					
					if(!response.rs)
					{
						$scope.p.iswork=false;
					}
					$scope.p.start=$scope.p.start+$scope.p.limit;
					
					$scope.callSuccessHttp();	
					
				})
				
				
			}
			
			
		},
		link:function(s,e,a,c){
			c.getlist(e.index()+1);
			
				
		}
	}
});


//下拉加载分页
app.directive('sqDropmoreajax',function(){
	return {
		controller:function($scope,$http,$routeParams){
			var _this=this;
			$scope.isloaded=true;
			this.getlist=function(){
				
//				alert('开始加载啦');
				var _link=buildUrl('json','moments','get_moments_list',{
					actId:$.util.getParamByObject("actId"),
					chanelId:$.util.getParamByObject("chanelId"),
					start:$scope.pageUi.start-$scope.pageUi.isTopCount,
					limit:$scope.pageUi.limit,
					momentsId:$routeParams.id
				})
//				$scope.uiObj.pageloadingCtrl=false;
				
				if($scope.pageUi.isWork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载...';
					$scope.isloaded=false;
					$http.get(_link).success(function(response){
						if(response.new_info)
						$scope.uiObj.new_info=response.new_info;
						if(response.list!=null)
						{

							for(var i=0;i<response.list.length;i++)
							{
								if(response.list[i].picUrls)
								{
									response.list[i].picUrls=response.list[i].picUrls.split(",");
								}
							}
							if($scope.pageUi.isFirst)
							{
								
								$scope.data = response;
								$scope.pageUi.isFirst=false;
							}
							else
							{
								for(var i=0;i<response.list.length;i++)
								{
									$scope.data.list.push(response.list[i]);
								}
							}
							
							console.log($scope);
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							if($routeParams.id)
							$scope.pageUi.text='没有更多数据';
							else
							$scope.pageUi.text='点击加载更多';
						}
						else
						{
							$scope.pageUi.text='没有更多数据';
							$scope.pageUi.isWork=false;
						}
						$scope.isloaded=true;
						$scope.callSuccessHttp();
					});
				}
			}
		},
		link:function(s,e,a,c){			
			c.getlist();
			//添加点击加载更多
			e.find('.sq-dropmore').on('click',function(){
				c.getlist();
			})
			//监听滚动条
			 window.onscroll = function()
			{
			　　  if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
				　　　　c.getlist();
				}
			}
		}
	}
})

//下拉加载分页
app.directive('sqNewsajax',function(){
	return {
		controller:function($scope,$http){
			var _this=this;
			$scope.isloaded=true;
			this.getlist=function(){
				
//				alert('开始加载啦');
				var _link=buildUrl('json','moments','get_new_info',{
					actId:$.util.getParamByObject("actId"),
					chanelId:$.util.getParamByObject("chanelId")
				})
//				$scope.uiObj.pageloadingCtrl=false;
				
				if($scope.pageUi.isWork&&$scope.isloaded)
				{
					$scope.pageUi.text='正在加载...';
					$scope.isloaded=false;
					$http.get(_link).success(function(response){
						if(response.new_info_list!=null)
						{
							if($scope.pageUi.isFirst)
							{
								
								$scope.data = response;
								$scope.pageUi.isFirst=false;
							}
							else
							{
								for(var i=0;i<response.new_info_list.length;i++)
								{
									$scope.data.new_info_list.push(response.new_info_list[i]);
								}
							}
							
							console.log($scope);
							$scope.pageUi.start=$scope.pageUi.start+$scope.pageUi.limit;
							$scope.pageUi.text='下拉加载';
						}
						else
						{
							$scope.pageUi.text='没有更多数据';
							$scope.pageUi.isWork=false;
						}
						$scope.isloaded=true;
						$scope.callSuccessHttp();
					});
				}
			}
		},
		link:function(s,e,a,c){			
			c.getlist();
			//添加点击加载更多
			e.find('.sq-dropmore').on('click',function(){
				c.getlist();
			})
			//监听滚动条
			 window.onscroll = function()
			{
			　　  if(getScrollTop() + getWindowHeight() == getScrollHeight()&&getScrollTop()!=0)
				{
				// 　　　　c.getlist();
				}
			}
		}
	}
})

